package com.es.www.second;

/**
 * @Created by michangtao
 * @Date 2022/5/25 11:19
 * @Description
 */
public class DefaultPipeline implements Pipeline {

    AbstractHandler head;
    AbstractHandler tail;

    public DefaultPipeline() {
        head = new DefaultHeadHandler();
        tail = new DefaultTailHandler();
        head.nextHandler = tail;
        tail.prevHandler = head;
    }

    @Override
    public DefaultPipeline addLast(AbstractHandler newHandler) {
        AbstractHandler prev = tail.prevHandler;
        prev.nextHandler = newHandler;
        newHandler.nextHandler = tail;
        newHandler.prevHandler = prev;
        tail.prevHandler = newHandler;
        return this;
    }

    @Override
    public void clear() {
        head.nextHandler = tail;
        tail.prevHandler = head;
    }

    @Override
    public AbstractHandler getHead() {
        return head;
    }

    @Override
    public AbstractHandler getTail() {
        return tail;
    }
}
